package com.vae.content.api;

import com.vae.base.model.page.PageParams;
import com.vae.base.model.page.PageResult;
import com.vae.base.utils.SecurityUtil;
import com.vae.content.domain.dto.AddCourseDto;
import com.vae.content.domain.dto.CourseBaseInfoDto;
import com.vae.content.domain.dto.EditCourseDto;
import com.vae.content.domain.dto.QueryCourseParamsDto;
import com.vae.content.domain.entity.CourseBase;
import com.vae.content.service.CourseBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: vae
 * 功能: 课程信息编辑接口
 * 时间: 2024-03-28 21:10
 */
@RefreshScope
@Api(value = "课程信息编辑",tags = "课程信息编辑")
@RestController
public class CourseBaseInfoController {
    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 课程信息分页条件查询
     * @param pageParams
     * @param dto
     * @return
     */
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    @ApiOperation("课程查询接口")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody QueryCourseParamsDto dto){
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        String companyId = user.getId();
        PageResult<CourseBase> pageResult = courseBaseService.queryCourseBaseList(companyId, pageParams,dto);
        return pageResult;
    }


    /**
     * 新增课程
     * @param addCourseDto
     * @return
     */
    @ApiOperation("新增课程")
    @PostMapping("/course")
    //@Validated注解的作用是开启参数校验，这样AddCourseDto类的@NotEmpty、@Size注解才会生效
    public CourseBaseInfoDto createCourseBase(@Validated @RequestBody AddCourseDto addCourseDto){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId = Long.valueOf(user.getId());
        CourseBaseInfoDto courseBase = courseBaseService.createCourseBase(companyId, addCourseDto);
        return courseBase;
    }

    /**
     * 根据课程id查询课程信息
     * @param courseId
     * @return
     */
    @ApiOperation("根据课程id查询接口")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        CourseBaseInfoDto courseBaseInfo = courseBaseService.getCourseBaseInfo(courseId);
        return courseBaseInfo;
    }

    /**
     * 修改课程
     * @param editCourseDto
     * @return
     */
    @ApiOperation("修改课程")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId = Long.valueOf(user.getId());
        CourseBaseInfoDto courseBaseInfoDto = courseBaseService.updateCourseBase(companyId, editCourseDto);
        return courseBaseInfoDto;
    }

}
